<Project>
  <PropertyGroup>
    <!-- Post Wasm MVP features -->
    <WasmEnableExceptionHandling Condition="'$(WasmEnableExceptionHandling)' == ''">true</WasmEnableExceptionHandling>
    <WasmEnableSIMD Condition="'$(WasmEnableSIMD)' == ''">$(WasmEnableExceptionHandling)</WasmEnableSIMD>
  </PropertyGroup>

  <Import Project="$(WasmCommonTargetsPath)WasmApp.Common.targets" />

  <UsingTask TaskName="Microsoft.WebAssembly.Build.Tasks.WasmAppBuilder" AssemblyFile="$(WasmAppBuilderTasksAssemblyPath)" />

  <PropertyGroup>
    <PrepareForWasmBuildNativeDependsOn>
      _CheckToolchainIsExpectedVersion;
      _PrepareForBrowserWasmBuildNative;
      $(PrepareForWasmBuildNativeDependsOn)
    </PrepareForWasmBuildNativeDependsOn>

    <WasmLinkDotNetDependsOn>
      $(WasmLinkDotNetDependsOn);
      _BrowserWasmLinkDotNet;
    </WasmLinkDotNetDependsOn>

    <WasmGenerateAppBundleDependsOn>
      $(WasmGenerateAppBundleDependsOn);
      _GetWasmGenerateAppBundleDependencies;
      _WasmGenerateAppBundle;
      _WasmGenerateRunV8Script;
    </WasmGenerateAppBundleDependsOn>

    <WasmEnableJsInteropByValue Condition="'$(WasmEnableJsInteropByValue)' == '' and '$(WasmEnableThreads)' == 'true'">true</WasmEnableJsInteropByValue>
    <WasmEnableJsInteropByValue Condition="'$(WasmEnableJsInteropByValue)' == ''">false</WasmEnableJsInteropByValue>

    <!-- Temporarily `false`, till sdk gets a fix for supporting the new file -->
    <WasmEmitSymbolMap Condition="'$(WasmEmitSymbolMap)' == '' and '$(RunAOTCompilation)' != 'true'">false</WasmEmitSymbolMap>

    <!-- by default, package assemblies as webcil -->
    <WasmEnableWebcil Condition="'$(WasmEnableWebcil)' == ''">true</WasmEnableWebcil>
    <WasmAssemblyExtension Condition="'$(WasmEnableWebcil)' == 'true'">.wasm</WasmAssemblyExtension>
    <WasmAssemblyExtension Condition="'$(WasmEnableWebcil)' != 'true'">.dll</WasmAssemblyExtension>

    <WasmStripILAfterAOT Condition="'$(WasmStripILAfterAOT)' == ''">true</WasmStripILAfterAOT>
    <WasmRuntimeAssetsLocation Condition="'$(WasmRuntimeAssetsLocation)' == ''">_framework</WasmRuntimeAssetsLocation>
    <TrimMode Condition="'$(TrimMode)' == ''">full</TrimMode>

    <_WasmGenerateAppBundleDependsOn>_GetWasmGenerateAppBundleDependencies;$(_WasmGenerateAppBundleDependsOn)</_WasmGenerateAppBundleDependsOn>

    <_ExtraTrimmerArgs Condition="'$(WasmEnableSIMD)' == 'true' and '$(RunAOTCompilation)' == 'true'">$(_ExtraTrimmerArgs) --substitutions "$(MSBuildThisFileDirectory)ILLink.Substitutions.WasmIntrinsics.xml"</_ExtraTrimmerArgs>
    <_ExtraTrimmerArgs Condition="'$(WasmEnableSIMD)' != 'true'">$(_ExtraTrimmerArgs) --substitutions "$(MSBuildThisFileDirectory)ILLink.Substitutions.NoWasmIntrinsics.xml"</_ExtraTrimmerArgs>

    <WasmUseEMSDK_PATH Condition="'$(WasmUseEMSDK_PATH)' == '' and '$(EMSDK_PATH)' != '' and Exists('$(MSBuildThisFileDirectory)WasmApp.InTree.targets')">true</WasmUseEMSDK_PATH>
    <WasmClang>emcc</WasmClang>

    <_WasmDefaultFlags Condition="'$(WasmEnableExceptionHandling)' == 'false'">-fexceptions</_WasmDefaultFlags>
    <_WasmDefaultFlags Condition="'$(WasmEnableExceptionHandling)' != 'false'">-fwasm-exceptions</_WasmDefaultFlags>
    <_WasmDefaultFlags Condition="'$(WasmEnableSIMD)' == 'true'">-msimd128</_WasmDefaultFlags>

    <_WasmOutputFileName Condition="'$(WasmSingleFileBundle)' != 'true'">dotnet.native.wasm</_WasmOutputFileName>
  </PropertyGroup>

  <ItemGroup>
    <!-- Allow running/debugging from VS -->
    <ProjectCapability Include="DotNetCoreWeb"/>

    <_BoolPropertiesThatTriggerRelinking Include="WasmEnableSIMD" DefaultValueInRuntimePack="true" />
    <_BoolPropertiesThatTriggerRelinking Include="WasmEnableExceptionHandling" DefaultValueInRuntimePack="true" />
  </ItemGroup>

  <Target Name="_GetWasmGenerateAppBundleDependencies">
    <Warning Condition="'$(InvariantGlobalization)' == 'true' and '$(HybridGlobalization)' == 'true'" Text="%24(HybridGlobalization) has no effect when %24(InvariantGlobalization) is set to true." />
    <Warning Condition="'$(WasmIcuDataFileName)' != '' and '$(HybridGlobalization)' == 'true'" Text="%24(WasmIcuDataFileName) has no effect when %24(HybridGlobalization) is set to true." />
    <Warning Condition="'$(InvariantGlobalization)' != 'true' and '$(WasmIncludeFullIcuData)' == 'true' and '$(WasmIcuDataFileName)' != ''" Text="%24(WasmIcuDataFileName) has no effect when %24(WasmIncludeFullIcuData) is set to true." />

    <PropertyGroup>
      <_HasDotnetWasm Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.native.wasm'">true</_HasDotnetWasm>
      <_HasDotnetJsWorker Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.native.worker.js'">true</_HasDotnetJsWorker>
      <_HasDotnetJsSymbols Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.native.js.symbols'">true</_HasDotnetJsSymbols>
      <_HasDotnetNativeJs Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.native.js'">true</_HasDotnetNativeJs>
      <HybridGlobalization Condition="'$(InvariantGlobalization)' == 'true'">false</HybridGlobalization>
      <_WasmIcuDataFileName Condition="'$(HybridGlobalization)' != 'true' and '$(WasmIcuDataFileName)' != '' and Exists('$(WasmIcuDataFileName)')">$(WasmIcuDataFileName)</_WasmIcuDataFileName>
      <_WasmIcuDataFileName Condition="'$(HybridGlobalization)' != 'true' and '$(WasmIcuDataFileName)' != '' and !Exists('$(WasmIcuDataFileName)')">$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(WasmIcuDataFileName)</_WasmIcuDataFileName>
    </PropertyGroup>

    <ItemGroup>
      <!-- If dotnet.{wasm,js} weren't added already (eg. AOT can add them), then add the default ones -->
      <WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.js" />
      <WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.runtime.js" />
      <WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.native.wasm" Condition="'$(_HasDotnetWasm)' != 'true'" />
      <WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.native.js" Condition="'$(_HasDotnetNativeJs)' != 'true'" />
      <WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.native.worker.js" Condition="'$(_HasDotnetJsWorker)' != 'true' and Exists('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.native.worker.js')" />
      <WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.native.js.symbols"
                       Condition="'$(WasmEmitSymbolMap)' == 'true' and
                                  '$(_HasDotnetJsSymbols)' != 'true' and
                                  Exists('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.native.js.symbols')" />
      <WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.js.map"
                       Condition="'$(WasmEmitSourceMap)' != 'false'" />
      <WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.runtime.js.map"
                       Condition="'$(WasmEmitSourceMap)' != 'false'" />
    </ItemGroup>

    <ItemGroup Condition="'$(InvariantGlobalization)' != 'true'">
      <_HybridGlobalizationDataFiles Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)icudt_hybrid.dat"/>
      <_HybridGlobalizationDataFiles Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)segmentation-rules.json"/>
      <_IcuAvailableDataFiles Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)icudt_*"  Exclude="@(_HybridGlobalizationDataFiles);$(_WasmIcuDataFileName)"/>
      <WasmIcuDataFileNames Condition="'$(HybridGlobalization)' == 'true'" Include="@(_HybridGlobalizationDataFiles)"/>
      <WasmIcuDataFileNames Condition="'$(HybridGlobalization)' != 'true' and '$(WasmIncludeFullIcuData)' == 'true'" Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)icudt.dat"/>
      <WasmIcuDataFileNames Condition="'$(HybridGlobalization)' != 'true' and '$(WasmIncludeFullIcuData)' != 'true' and '$(_WasmIcuDataFileName)' == ''" Include="@(_IcuAvailableDataFiles)"/>
      <WasmIcuDataFileNames Condition="'$(HybridGlobalization)' != 'true' and '$(WasmIncludeFullIcuData)' != 'true' and '$(_WasmIcuDataFileName)' != ''" Include="$(_WasmIcuDataFileName)"/>
      <WasmNativeAsset Include="@(WasmIcuDataFileNames)"/>
    </ItemGroup>

    <Error Condition="'$(InvariantGlobalization)' != 'true' and '$(WasmIncludeFullIcuData)' != 'true' and '$(WasmIcuDataFileName)' != '' and !Exists('$(_WasmIcuDataFileName)')" Text="File in location %24(WasmIcuDataFileName)=$(WasmIcuDataFileName) cannot be found neither when used as absolute path nor a relative runtime pack path. You might want to pass one of the values: @(_IcuAvailableDataFiles->'%(Filename)')."/>
    <Error Condition="'$(InvariantGlobalization)' != 'true' and '$(WasmIncludeFullIcuData)' != 'true' and '$(WasmIcuDataFileName)' != '' and !$([System.IO.Path]::GetFileName('$(WasmIcuDataFileName)').StartsWith('icudt'))" Text="Custom ICU file name in path %24(WasmIcuDataFileName)=$(WasmIcuDataFileName) must start with 'icudt'."/>
  </Target>

  <Target Name="_WasmGenerateAppBundle"
          Inputs="@(_WasmAssembliesInternal);@(WasmExtraFilesToDeploy);$(WasmIcuDataFileName);@(WasmNativeAsset)"
          Outputs="$(WasmAppDir)\.stamp"
          Condition="'$(WasmGenerateAppBundle)' == 'true'">

    <PropertyGroup>
      <!-- TODO: set this from some user-facing property?  -1 means use the default baked into dotnet.native.js -->
      <_WasmPThreadPoolInitialSize Condition="'$(_WasmPThreadPoolInitialSize)' == ''">-1</_WasmPThreadPoolInitialSize>
      <_WasmPThreadPoolUnusedSize Condition="'$(_WasmPThreadPoolUnusedSize)' == ''">-1</_WasmPThreadPoolUnusedSize>
    </PropertyGroup>

    <ItemGroup>
      <WasmExtraFilesToDeploy Include="$(WasmMainHTMLPath)" Condition="'$(WasmMainHTMLPath)' != ''" />
      <WasmExtraFilesToDeploy Include="$(WasmMainJSPath)" Condition="'$(WasmMainJSPath)' != ''" />
    </ItemGroup>

    <RemoveDir Directories="$(WasmAppDir)" />
    <WasmAppBuilder
      AppDir="$(WasmAppDir)"
      Assemblies="@(_WasmAssembliesInternal)"
      MainAssemblyName="$(WasmMainAssemblyFileName)"
      HostConfigs="@(HostConfig)"
      RuntimeArgsForHost="@(WasmMonoRuntimeArgs)"
      DefaultHostConfig="$(DefaultWasmHostConfig)"
      InvariantGlobalization="$(InvariantGlobalization)"
      HybridGlobalization="$(HybridGlobalization)"
      SatelliteAssemblies="@(_WasmSatelliteAssemblies)"
      FilesToIncludeInFileSystem="@(WasmFilesToIncludeInFileSystem)"
      IcuDataFileNames="@(WasmIcuDataFileNames)"
      RemoteSources="@(WasmRemoteSources)"
      ExtraFilesToDeploy="@(WasmExtraFilesToDeploy)"
      ExtraConfig="@(WasmExtraConfig)"
      NativeAssets="@(WasmNativeAsset)"
      DebugLevel="$(WasmDebugLevel)"
      IncludeThreadsWorker="$(WasmEnableThreads)"
      PThreadPoolInitialSize="$(_WasmPThreadPoolInitialSize)"
      PThreadPoolUnusedSize="$(_WasmPThreadPoolUnusedSize)"
      UseWebcil="$(WasmEnableWebcil)"
      WasmIncludeFullIcuData="$(WasmIncludeFullIcuData)"
      WasmIcuDataFileName="$(WasmIcuDataFileName)"
      RuntimeAssetsLocation="$(WasmRuntimeAssetsLocation)"
      CacheBootResources="$(BlazorCacheBootResources)"
      RuntimeConfigJsonPath="$(_WasmRuntimeConfigFilePath)"
      >
      <Output TaskParameter="FileWrites" ItemName="FileWrites" />
    </WasmAppBuilder>

    <Message Text="Generated app bundle at $(WasmAppDir)" Importance="High" />

    <Copy SourceFiles="@(_WasmAppHostFiles)" DestinationFiles="@(_WasmAppHostFiles->'$(WasmAppDir)\WasmAppHost\%(RecursiveDir)%(FileName)%(Extension)')" />

    <WriteLinesToFile File="$(WasmAppDir)\.stamp" Lines="" Overwrite="true" />
  </Target>

  <Import Project="$(MSBuildThisFileDirectory)EmSdkRepo.Defaults.props" Condition="'$(WasmUseEMSDK_PATH)' == 'true'" />
  <Target Name="_SetupEmscripten" BeforeTargets="_SetupToolchain">
    <PropertyGroup>
      <_EMSDKMissingPaths Condition="'$(_EMSDKMissingPaths)' == '' and ('$(EmscriptenSdkToolsPath)' == '' or !Exists('$(EmscriptenSdkToolsPath)'))">%24(EmscriptenSdkToolsPath)=$(EmscriptenSdkToolsPath) </_EMSDKMissingPaths>
      <_EMSDKMissingPaths Condition="'$(_EMSDKMissingPaths)' == '' and ('$(EmscriptenNodeToolsPath)' == '' or !Exists('$(EmscriptenNodeToolsPath)'))">%24(EmscriptenNodeToolsPath)=$(EmscriptenNodeToolsPath) </_EMSDKMissingPaths>
      <_EMSDKMissingPaths Condition="'$(_EMSDKMissingPaths)' == '' and ('$(EmscriptenUpstreamBinPath)' == '' or !Exists('$(EmscriptenUpstreamBinPath)'))">%24(EmscriptenUpstreamBinPath)=$(EmscriptenUpstreamBinPath) </_EMSDKMissingPaths>
    </PropertyGroup>

    <PropertyGroup>
      <_ToolchainMissingErrorMessage Condition="'$(EMSDK_PATH)' == '' and '$(EmscriptenSdkToolsPath)' == ''">Could not find emscripten sdk. Either set %24(EMSDK_PATH), or use workloads to get the sdk.</_ToolchainMissingErrorMessage>

      <_ToolchainMissingErrorMessage Condition="'$(_ToolchainMissingErrorMessage)' == '' and '$(_UsingEMSDK_PATH)' != 'true' and '$(_EMSDKMissingPaths)' != ''">Emscripten from the workload is missing some paths: $(_EMSDKMissingPaths).</_ToolchainMissingErrorMessage>
      <_ToolchainMissingErrorMessage Condition="'$(_ToolchainMissingErrorMessage)' == '' and '$(_UsingEMSDK_PATH)' == 'true' and !Exists($(EMSDK_PATH))">Could not find Emscripten sdk at %24(EMSDK_PATH)=$(EMSDK_PATH) .</_ToolchainMissingErrorMessage>
      <_ToolchainMissingErrorMessage Condition="'$(_ToolchainMissingErrorMessage)' == '' and '$(_UsingEMSDK_PATH)' == 'true' and '$(_EMSDKMissingPaths)' != ''">Specified Emscripten sdk at %24(EMSDK_PATH)=$(EMSDK_PATH) is missing some paths: $(_EMSDKMissingPaths).</_ToolchainMissingErrorMessage>

      <_IsToolchainMissing Condition="'$(_ToolchainMissingErrorMessage)' != ''">true</_IsToolchainMissing>
    </PropertyGroup>

    <PropertyGroup>
      <EmscriptenSdkToolsPath    Condition="'$(EmscriptenSdkToolsPath)' != ''"   >$([MSBuild]::NormalizeDirectory($(EmscriptenSdkToolsPath)))</EmscriptenSdkToolsPath>
      <EmscriptenNodeToolsPath   Condition="'$(EmscriptenNodeToolsPath)' != ''"  >$([MSBuild]::NormalizeDirectory($(EmscriptenNodeToolsPath)))</EmscriptenNodeToolsPath>
      <EmscriptenUpstreamBinPath Condition="'$(EmscriptenUpstreamBinPath)' != ''">$([MSBuild]::NormalizeDirectory($(EmscriptenUpstreamBinPath)))</EmscriptenUpstreamBinPath>
    </PropertyGroup>

    <!-- Environment variables required for running emsdk commands like `emcc` -->
    <ItemGroup Condition="'$(EmscriptenSdkToolsPath)' != ''">
      <EmscriptenEnvVars Include="DOTNET_EMSCRIPTEN_LLVM_ROOT=$(EmscriptenSdkToolsPath)bin" />
      <EmscriptenEnvVars Include="DOTNET_EMSCRIPTEN_BINARYEN_ROOT=$(EmscriptenSdkToolsPath)" />
      <EmscriptenEnvVars Include="DOTNET_EMSCRIPTEN_NODE_JS=$([MSBuild]::NormalizePath($(EmscriptenNodeToolsPath), 'bin', 'node$(_ExeExt)'))" />
    </ItemGroup>

    <ItemGroup>
      <_EmscriptenPrependPATHTrimmed Include="$([MSBuild]::ValueOrDefault('%(EmscriptenPrependPATH.Identity)\', '').TrimEnd('\/'))" />
      <EmscriptenPrependPATH Remove="@(EmscriptenPrependPATH)" />
      <EmscriptenPrependPATH Include="@(_EmscriptenPrependPATHTrimmed)" />
    </ItemGroup>

    <PropertyGroup>
      <_EmscriptenPrependPATHProperty>@(EmscriptenPrependPATH -> '%(Identity)', '$(_PathSeparator)')</_EmscriptenPrependPATHProperty>
    </PropertyGroup>
    <ItemGroup>
      <EmscriptenEnvVars Include="PATH=$(_EmscriptenPrependPATHProperty)$(_PathSeparator)$([MSBuild]::Escape($(PATH)))" />

      <!--
        Python defaults to the system charset, and thus expects the files it's reading to
        match that. But that might not always be true. Eg. system charset=gbk, failing to read
        utf-8 files

        See https://github.com/dotnet/runtime/issues/53367 for the motivating issue
      -->
      <EmscriptenEnvVars Include="PYTHONUTF8=1" />
      <EmscriptenEnvVars Include="EM_WORKAROUND_PYTHON_BUG_34780=1" />
    </ItemGroup>

    <PropertyGroup>
      <_WasmLLVMPathForAOT>$(EmscriptenUpstreamBinPath)</_WasmLLVMPathForAOT>
    </PropertyGroup>
  </Target>

  <Target Name="_PrepareForBrowserWasmBuildNative">
    <Error Condition="'$(_IsToolchainMissing)' == 'true'"
           Text="$(_ToolchainMissingErrorMessage) Emscripten SDK is required for building native files." />

    <PropertyGroup>
      <_MonoAotCrossCompilerPath>@(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier', $(RuntimeIdentifier)))</_MonoAotCrossCompilerPath>
      <_WasmDefaultFlagsRsp>$([MSBuild]::NormalizePath($(_WasmRuntimePackSrcDir), 'emcc-default.rsp'))</_WasmDefaultFlagsRsp>
      <_WasmDefaultLinkFlagsRsp>$([MSBuild]::NormalizePath($(_WasmRuntimePackSrcDir), 'emcc-link.rsp'))</_WasmDefaultLinkFlagsRsp>
      <!--<_WasmLLVMPathForAOT>$(EmscriptenUpstreamBinPath)</_WasmLLVMPathForAOT>-->
      <WasmLinkIcalls Condition="'$(WasmLinkIcalls)' == ''">$(WasmBuildNative)</WasmLinkIcalls>
      <WasmRunWasmOpt Condition="'$(WasmRunWasmOpt)' == '' and '$(WasmNativeStrip)' == 'true'">true</WasmRunWasmOpt>
      <WasmRunWasmOpt Condition="'$(WasmRunWasmOpt)' == ''">false</WasmRunWasmOpt>

      <_WasmICallTablePath>$(_WasmIntermediateOutputPath)icall-table.h</_WasmICallTablePath>
      <_WasmRuntimeICallTablePath>$(_WasmIntermediateOutputPath)runtime-icall-table.h</_WasmRuntimeICallTablePath>
      <_WasmPInvokeTablePath>$(_WasmIntermediateOutputPath)pinvoke-table.h</_WasmPInvokeTablePath>
      <_WasmInterpToNativeTablePath>$(_WasmIntermediateOutputPath)wasm_m2n_invoke.g.h</_WasmInterpToNativeTablePath>
      <_WasmM2NCachePath>$(_WasmIntermediateOutputPath)m2n_cache.txt</_WasmM2NCachePath>
      <_WasmPInvokeHPath>$(_WasmRuntimePackIncludeDir)wasm\pinvoke.h</_WasmPInvokeHPath>
      <_DriverGenCPath>$(_WasmIntermediateOutputPath)driver-gen.c</_DriverGenCPath>
      <DisableParallelAot Condition="'$(DisableParallelAot)' == ''">false</DisableParallelAot>
      <DisableParallelEmccCompile Condition="'$(DisableParallelEmccCompile)' == ''">$(DisableParallelAot)</DisableParallelEmccCompile>

      <_DriverGenCNeeded Condition="'$(_DriverGenCNeeded)' == '' and '$(_WasmShouldAOT)' == 'true'">true</_DriverGenCNeeded>

      <_WasmDevel Condition="'$(_WasmDevel)' == '' and '$(WasmBuildNative)' == 'true' and '$(Configuration)' == 'Debug' and '$(WasmBuildingForNestedPublish)' != 'true'">true</_WasmDevel>

      <_EmccOptimizationFlagDefault Condition="'$(_WasmDevel)' == 'true'">-O0</_EmccOptimizationFlagDefault>
      <_EmccOptimizationFlagDefault Condition="'$(_EmccOptimizationFlagDefault)' == '' and '$(Configuration)' == 'Debug' and '$(WasmBuildingForNestedPublish)' != 'true'">-O1</_EmccOptimizationFlagDefault>
      <_EmccOptimizationFlagDefault Condition="'$(_EmccOptimizationFlagDefault)' == ''">-Oz</_EmccOptimizationFlagDefault>

      <EmccCompileOptimizationFlag Condition="'$(EmccCompileOptimizationFlag)' == ''">$(_EmccOptimizationFlagDefault)</EmccCompileOptimizationFlag>
      <!-- FIXME: which one should take precedence? -->
      <WasmCompileOptimizationFlag>$(EmccCompileOptimizationFlag)</WasmCompileOptimizationFlag>
      <EmccLinkOptimizationFlag    Condition="'$(EmccLinkOptimizationFlag)' == '' and '$(Configuration)' == 'Release'">-O2</EmccLinkOptimizationFlag>
      <EmccLinkOptimizationFlag    Condition="'$(EmccLinkOptimizationFlag)' == ''"   >$(WasmCompileOptimizationFlag)</EmccLinkOptimizationFlag>
      <WasmLinkOptimizationFlag>$(EmccLinkOptimizationFlag)</WasmLinkOptimizationFlag>

      <_WasmCompileRsp>$(_WasmIntermediateOutputPath)emcc-compile.rsp</_WasmCompileRsp>
      <_WasmCompileOutputMessageImportance Condition="'$(EmccVerbose)' == 'true'">Normal</_WasmCompileOutputMessageImportance>
      <_WasmCompileOutputMessageImportance Condition="'$(EmccVerbose)' != 'true'">Low</_WasmCompileOutputMessageImportance>

      <_WasmCompileBitcodeRsp>$(_WasmIntermediateOutputPath)emcc-compile-bc.rsp</_WasmCompileBitcodeRsp>
      <_WasmLinkRsp>$(_WasmIntermediateOutputPath)emcc-link.rsp</_WasmLinkRsp>

      <EmccInitialHeapSize Condition="'$(EmccInitialHeapSize)' == ''">$(EmccTotalMemory)</EmccInitialHeapSize>
      <WasmInitialHeapSize Condition="'$(WasmInitialHeapSize)' == ''">$(EmccInitialHeapSize)</WasmInitialHeapSize>
      <EmccStackSize Condition="'$(EmccStackSize)' == ''">5MB</EmccStackSize>
      <WasmAllowUndefinedSymbols Condition="'$(WasmAllowUndefinedSymbols)' == ''">false</WasmAllowUndefinedSymbols>

      <WasmClang>$(EmscriptenUpstreamEmscriptenPath)emcc</WasmClang>
    </PropertyGroup>

    <ItemGroup>
      <_WasmLinkDependencies Remove="@(_WasmLinkDependencies)" />

      <_EmccCommonFlags Include="$(_DefaultEmccFlags)" />
      <_EmccCommonFlags Include="$(EmccFlags)" />
      <_EmccCommonFlags Include="-g"                                       Condition="'$(WasmNativeStrip)' == 'false'" />
      <_EmccCommonFlags Include="-v"                                       Condition="'$(EmccVerbose)' != 'false'" />
      <_EmccCommonFlags Include="-s DISABLE_EXCEPTION_CATCHING=0"          Condition="'$(WasmEnableExceptionHandling)' == 'false'" />
      <_EmccCommonFlags Include="-fwasm-exceptions"                        Condition="'$(WasmEnableExceptionHandling)' == 'true'" />
      <_EmccCommonFlags Include="-s MAXIMUM_MEMORY=$(EmccMaximumHeapSize)" Condition="'$(EmccMaximumHeapSize)' != ''" />

      <_EmccIncludePaths Include="$(_WasmIntermediateOutputPath.TrimEnd('\/'))" />
      <_EmccIncludePaths Include="$(_WasmRuntimePackIncludeDir)mono-2.0" />
      <_EmccIncludePaths Include="$(_WasmRuntimePackIncludeDir)wasm" />

      <!-- Adding optimization flag at the top, so it gets precedence -->
      <_EmccCFlags Include="$(WasmCompileOptimizationFlag)" />
      <_EmccCFlags Include="@(_EmccCommonFlags)" />

      <_EmccCFlags Include="-DDISABLE_PERFTRACING_LISTEN_PORTS=1" />
      <_EmccCFlags Include="-DENABLE_AOT=1"                    Condition="'$(_WasmShouldAOT)' == 'true'" />
      <_EmccCFlags Include="-DDRIVER_GEN=1"                    Condition="'$(_WasmShouldAOT)' == 'true'" />
      <_EmccCFlags Include="-DINVARIANT_GLOBALIZATION=1"       Condition="'$(InvariantGlobalization)' == 'true'" />
      <_EmccCFlags Include="-DINVARIANT_TIMEZONE=1"            Condition="'$(InvariantTimezone)' == 'true'" />
      <_EmccCFlags Include="-DLINK_ICALLS=1"                   Condition="'$(WasmLinkIcalls)' == 'true'" />
      <_EmccCFlags Include="-DENABLE_AOT_PROFILER=1"           Condition="$(WasmProfilers.Contains('aot'))" />
      <_EmccCFlags Include="-DENABLE_BROWSER_PROFILER=1"       Condition="$(WasmProfilers.Contains('browser'))" />
      <_EmccCFlags Include="-DENABLE_JS_INTEROP_BY_VALUE=1"    Condition="'$(WasmEnableJsInteropByValue)' == 'true'" />

      <_EmccCFlags Include="-DGEN_PINVOKE=1" />
      <_EmccCFlags Include="-emit-llvm" />

      <_EmccCFlags Include="&quot;-I%(_EmccIncludePaths.Identity)&quot;" />
      <_EmccCFlags Include="-g"                                Condition="'$(WasmNativeDebugSymbols)' == 'true'" />

      <!-- Adding optimization flag at the top, so it gets precedence -->
      <_EmccLDFlags Include="$(WasmLinkOptimizationFlag)" />
      <_EmccLDFlags Include="@(_EmccCommonFlags)" />
      <_EmccLDFlags Include="-s EXPORT_ES6=1 -lexports.js" />
      <_EmccLDFlags Condition="'$(WasmEnableExceptionHandling)' != 'false'" Include="-s EXPORT_EXCEPTION_HANDLING_HELPERS=1"   />

      <_RuntimeCDependencies Include="$(_WasmPInvokeHPath);$(_WasmICallTablePath)" />
      <_RuntimeCDependencies Include="$(_DriverGenCPath)" Condition="'$(_DriverGenCNeeded)' == 'true'" />

      <_WasmRuntimePackSrcFile Include="$(_WasmRuntimePackSrcDir)pinvoke.c"
                               Dependencies="$(_WasmPInvokeHPath);$(_WasmPInvokeTablePath)" />
      <_WasmRuntimePackSrcFile Include="$(_WasmRuntimePackSrcDir)driver.c"
                               Dependencies="" />
      <_WasmRuntimePackSrcFile Include="$(_WasmRuntimePackSrcDir)runtime.c"
                               Dependencies="@(_RuntimeCDependencies)" />
      <_WasmRuntimePackSrcFile Include="$(_WasmRuntimePackSrcDir)corebindings.c" />

      <_WasmRuntimePackSrcFile ObjectFile="$(_WasmIntermediateOutputPath)%(FileName).o" />

      <!-- See src\mono\browser\runtime\modularize-dotnet.md -->
      <JSFileType Include="extpre.js"  Kind="extern-pre-js" />
      <JSFileType Include="iffe.js"    Kind="extern-pre-js" />
      <JSFileType Include="pre.js"     Kind="pre-js" />
      <JSFileType Include="lib.js"     Kind="js-library" />
      <JSFileType Include="post.js"    Kind="post-js" />
      <JSFileType Include="extpost.js" Kind="extern-post-js" />

      <_WasmExtraJSFile Include="$(_WasmRuntimePackSrcDir)\*.%(JSFileType.Identity)"     Kind="%(JSFileType.Kind)" />
      <_WasmExtraJSFile Include="$(_WasmRuntimePackSrcDir)\es6\*.%(JSFileType.Identity)" Kind="%(JSFileType.Kind)" />

      <_WasmNativeFileForLinking Include="@(NativeFileReference)" />

      <EmscriptenEnvVars Include="EMSDK_PYTHON=$(EmscriptenPythonToolsPath)python.exe" Condition="'$(OS)' == 'Windows_NT'" />
      <EmscriptenEnvVars Include="PYTHONPATH=$(EmscriptenPythonToolsPath)" Condition="'$(OS)' == 'Windows_NT'" />
      <EmscriptenEnvVars Include="PYTHONHOME=" Condition="'$(OS)' == 'Windows_NT'" />
      <EmscriptenEnvVars Include="EM_CACHE=$(WasmCachePath)" Condition="'$(WasmCachePath)' != ''" />
      <EmscriptenEnvVars Include="EM_FROZEN_CACHE=True" Condition="'$(WasmCachePath)' == '$(EmscriptenCacheSdkCacheDir)'" />
      <EmscriptenEnvVars Include="ENABLE_JS_INTEROP_BY_VALUE=1" Condition="'$(WasmEnableJsInteropByValue)' == 'true'" />
      <EmscriptenEnvVars Include="ENABLE_JS_INTEROP_BY_VALUE=0" Condition="'$(WasmEnableJsInteropByValue)' != 'true'" />
      <EmscriptenEnvVars Include="WASM_ENABLE_SIMD=1" Condition="'$(WasmEnableSIMD)' != 'false'" />
      <EmscriptenEnvVars Include="WASM_ENABLE_SIMD=0" Condition="'$(WasmEnableSIMD)' == 'false'" />
      <EmscriptenEnvVars Include="WASM_ENABLE_EH=1" Condition="'$(WasmEnableExceptionHandling)' != 'false'" />
      <EmscriptenEnvVars Include="WASM_ENABLE_EH=0" Condition="'$(WasmEnableExceptionHandling)' == 'false'" />
      <EmscriptenEnvVars Include="ENABLE_AOT_PROFILER=$([System.Convert]::ToInt32($(WasmProfilers.Contains('aot'))))" />
      <EmscriptenEnvVars Include="ENABLE_BROWSER_PROFILER=$([System.Convert]::ToInt32($(WasmProfilers.Contains('browser'))))" />
      <EmscriptenEnvVars Include="RUN_AOT_COMPILATION=1" Condition="'$(RunAOTCompilation)' == 'true'" />
      <EmscriptenEnvVars Include="RUN_AOT_COMPILATION=0" Condition="'$(RunAOTCompilation)' != 'true'" />

      <WasmToolchainEnvVars Include="@(EmscriptenEnvVars)" />
    </ItemGroup>

    <ItemGroup Condition="'$(WasmAllowUndefinedSymbols)' == 'true'">
      <_EmccLDSFlags Include="-Wl,--allow-undefined" />
      <_EmccLDSFlags Include="-s ERROR_ON_UNDEFINED_SYMBOLS=0" />
    </ItemGroup>
    <ItemGroup Condition="'$(WasmAllowUndefinedSymbols)' != 'true'">
      <_EmccLDSFlags Include="-s LLD_REPORT_UNDEFINED" />
      <_EmccLDSFlags Include="-s ERROR_ON_UNDEFINED_SYMBOLS=1" />
    </ItemGroup>

    <ItemGroup Condition="'$(WasmSingleFileBundle)' == 'true'">
      <_WasmBundleItem
                Include="@(_WasmAssembliesInternal)"
                BundleRegistrationFunctionName="mono_register_assemblies_bundle"
                BundleFile="wasm_bundled_assemblies.o" />

      <!-- TODO for single file bundle -->
      <!--<_WasmBundleItem-->
                <!--Include="@(WasmIcuDataFileNames)"-->
                <!--BundleRegistrationFunctionName="mono_register_icu_bundle"-->
                <!--BundleFile="wasm_bundled_icu.o" />-->

      <!--<_WasmBundleItem-->
                <!--Include="$(_ParsedRuntimeConfigFilePath)"-->
                <!--BundleRegistrationFunctionName="mono_register_runtimeconfig_bin"-->
                <!--BundleFile="wasm_bundled_runtimeconfig_bin.o" />-->
    </ItemGroup>

    <MakeDir Directories="$(WasmCachePath)" Condition="'$(WasmCachePath)' != '' and !Exists($(WasmCachePath))" />

    <Error Text="Could not find NativeFileReference %(NativeFileReference.Identity)" Condition="'%(NativeFileReference.Identity)' != '' and !Exists(%(NativeFileReference.Identity))" />
  </Target>

  <Target Name="_BrowserWasmWriteCompileRsp" BeforeTargets="_WasmWriteRspForCompilingNativeSourceFiles">
    <ItemGroup>
      <_EmccCFlags Include="$(EmccExtraCFlags)" />
      <_WasmCFlags Include="@(_EmccCFlags)" />
    </ItemGroup>
  </Target>

  <Target Name="_BrowserWasmBeforeWriteCompileBitcodeRsp" BeforeTargets="_WasmWriteRspForCompilingBitcode">
    <ItemGroup>
      <_BitcodeLDFlags Include="@(_EmccLDFlags)" />
      <_BitcodeLDFlags Include="$(EmccExtraBitcodeLDFlags)" />
    </ItemGroup>
  </Target>

  <Target Name="_BrowserWasmWriteRspForLinking" BeforeTargets="_WasmWriteRspForLinking">
    <PropertyGroup>
      <_WasmEHLib Condition="'$(WasmEnableExceptionHandling)' == 'true'">libmono-wasm-eh-wasm.a</_WasmEHLib>
      <_WasmEHLib Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-js.a</_WasmEHLib>
      <_WasmEHLibToExclude Condition="'$(WasmEnableExceptionHandling)' == 'true'">libmono-wasm-eh-js.a</_WasmEHLibToExclude>
      <_WasmEHLibToExclude Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-wasm.a</_WasmEHLibToExclude>
      <_WasmSIMDLib Condition="'$(WasmEnableSIMD)' == 'true'">libmono-wasm-simd.a</_WasmSIMDLib>
      <_WasmSIMDLib Condition="'$(WasmEnableSIMD)' != 'true'">libmono-wasm-nosimd.a</_WasmSIMDLib>
      <_WasmSIMDLibToExclude Condition="'$(WasmEnableSIMD)' != 'true'">libmono-wasm-simd.a</_WasmSIMDLibToExclude>
      <_WasmSIMDLibToExclude Condition="'$(WasmEnableSIMD)' == 'true'">libmono-wasm-nosimd.a</_WasmSIMDLibToExclude>
      <_EmccExportedLibraryFunction>"[@(EmccExportedLibraryFunction -> '%27%(Identity)%27', ',')]"</_EmccExportedLibraryFunction>
      <_EmccExportedRuntimeMethods>"[@(EmccExportedRuntimeMethod -> '%27%(Identity)%27', ',')]"</_EmccExportedRuntimeMethods>
      <_EmccExportedFunctions>@(EmccExportedFunction -> '%(Identity)',',')</_EmccExportedFunctions>
    </PropertyGroup>
    <ItemGroup>
      <!-- order matters -->
      <!-- some flags will be duplicated on the commandline and in the .rsp file. The last wins. -->
      <_EmccLDSFlags Include="-s INITIAL_MEMORY=$(WasmInitialHeapSize)" />
      <_EmccLDSFlags Include="-s STACK_SIZE=$(EmccStackSize)" />
      <_EmccLDSFlags Include="-s WASM_BIGINT=1" />
      <_EmccLDSFlags Condition="'$(EmccEnvironment)' != ''" Include="-s ENVIRONMENT=&quot;$(EmccEnvironment)&quot;" />
      <_EmccLDSFlags Condition="'$(EmccEnableAssertions)' == 'true'" Include="-s ASSERTIONS=1" />

      <_WasmNativeFileForLinking Include="%(_BitcodeFile.ObjectFile)" />
      <_WasmNativeFileForLinking Include="%(_WasmSourceFileToCompile.ObjectFile)" />
      <_WasmNativeFileForLinking Include="@(_WasmObjectFilesForBundle)" />

      <_WasmNativeFileForLinking
          Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)*.a"
          Exclude="@(_MonoRuntimeComponentDontLink->'$(MicrosoftNetCoreAppRuntimePackRidNativeDir)%(Identity)')" />
      <_WasmNativeFileForLinking Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmEHLib)" />
      <_WasmNativeFileForLinking Condition="'$(_WasmSIMDLib)' != ''" Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmSIMDLib)" />
      <_WasmNativeFileForLinking Remove="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmEHLibToExclude)" />
      <_WasmNativeFileForLinking Remove="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmSIMDLibToExclude)" />

      <_WasmExtraJSFile Include="@(Content)" Condition="'%(Content.Extension)' == '.js'" />

      <_EmccLinkStepArgs Include="@(_EmccLDFlags)" />
      <_EmccLinkStepArgs Include="@(_EmccLDSFlags)" />
      <_EmccLinkStepArgs Include="--emit-symbol-map" Condition="'$(WasmEmitSymbolMap)' == 'true'" />

      <_EmccLinkStepArgs Include="--%(_WasmExtraJSFile.Kind) &quot;%(_WasmExtraJSFile.Identity)&quot;" Condition="'%(_WasmExtraJSFile.Kind)' != ''" />
      <_WasmLinkDependencies Include="@(_WasmExtraJSFile)" />

      <_EmccLinkStepArgs Include="&quot;%(_WasmNativeFileForLinking.Identity)&quot;" />
      <_WasmLinkDependencies Include="@(_WasmNativeFileForLinking)" />

      <_EmccLinkStepArgs Include="-o &quot;$(_WasmIntermediateOutputPath)dotnet.native.js&quot;" />
      <_WasmLinkDependencies Include="$(_WasmLinkRsp)" />

      <_EmccLinkStepArgs Include="-s DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=$(_EmccExportedLibraryFunction)" Condition="'$(_EmccExportedLibraryFunction)' != ''" />
      <_EmccLinkStepArgs Include="-s EXPORTED_RUNTIME_METHODS=$(_EmccExportedRuntimeMethods)" />
      <_EmccLinkStepArgs Include="-s EXPORTED_FUNCTIONS=$(_EmccExportedFunctions)" />

      <_EmccLinkStepArgs Include="$(EmccExtraLDFlags)" />

      <_WasmLinkStepArgs Include="@(_EmccLinkStepArgs)" />
    </ItemGroup>
  </Target>

  <Target Name="_BrowserWasmLinkDotNet"
          Inputs="@(_WasmLinkDependencies);$(_WasmDefaultFlagsRsp);$(_WasmDefaultLinkFlagsRsp);$(_WasmLinkRsp)"
          Outputs="$(_WasmIntermediateOutputPath)dotnet.native.js;$(_WasmIntermediateOutputPath)dotnet.native.wasm"
          Returns="@(FileWrites)" >

    <Message Text="Linking for initial memory %24(WasmInitialHeapSize)=$(WasmInitialHeapSize) bytes. Set this msbuild property to change the value." Importance="High" />
    <Message Text="Linking with emcc with $(WasmLinkOptimizationFlag). This may take a while ..." Importance="High" />
    <Message Text="Running emcc with @(_EmccLinkStepArgs->'%(Identity)', ' ')" Importance="Low" />
    <Exec Command='emcc "@$(_WasmDefaultFlagsRsp)" $(_WasmDefaultFlags) "@$(_WasmDefaultLinkFlagsRsp)" "@$(_WasmLinkRsp)"' EnvironmentVariables="@(EmscriptenEnvVars)" ConsoleToMSBuild="true">
      <Output TaskParameter="ConsoleOutput" PropertyName="_EmccLinkStepConsoleOutput" />
      <Output TaskParameter="ExitCode" PropertyName="_EmccLinkStepExitCode" />
    </Exec>
    <ItemGroup>
      <FileWrites Include="$(_WasmIntermediateOutputPath)dotnet.native.wasm" />
      <FileWrites Include="$(_WasmIntermediateOutputPath)dotnet.native.js" />
      <FileWrites Include="$(_WasmIntermediateOutputPath)dotnet.native.js.symbols" Condition="'$(WasmEmitSymbolMap)' == 'true'" />
    </ItemGroup>

    <CallTarget Targets="_RunWasmOptPostLink" Condition="'$(WasmRunWasmOpt)' == 'true'" />

    <OnError ExecuteTargets="_OnError_WasmLinkDotNet_UndefinedSymbols"
             Condition="'$(WasmAllowUndefinedSymbols)' != 'true' and '$(_EmccLinkStepExitCode)' != '0' and $([System.Text.RegularExpressions.Regex]::IsMatch($(_EmccLinkStepConsoleOutput), 'wasm-ld *: *error *:.*undefined symbol:'))" />
  </Target>
  <Target Name="_OnError_WasmLinkDotNet_UndefinedSymbols">
    <Error Text="Use '-p:WasmAllowUndefinedSymbols=true' to allow undefined symbols" />
  </Target>

  <Target Name="_CheckEmccIsExpectedVersion" BeforeTargets="_CheckToolchainIsExpectedVersion" Condition="'$(_WasmStrictVersionMatch)' == 'true'">
    <Error Condition="'$(_IsToolchainMissing)' == 'true'"
           Text="$(_ToolchainMissingErrorMessage) Emscripten SDK is required for building native files." />
    <Error Condition="'$(RuntimeEmccVersionRaw)' == ''"
           Text="%24(RuntimeEmccVersionRaw) is not set. '$(_EmccPropsPath)' should have set that."/>

    <PropertyGroup>
      <_EmccVersionCommand>emcc --version</_EmccVersionCommand>
    </PropertyGroup>

    <Exec Command="$(_EmccVersionCommand)" WorkingDirectory="$(_WasmIntermediateOutputPath)" EnvironmentVariables="@(EmscriptenEnvVars)" ConsoleToMsBuild="true" StandardOutputImportance="Low" IgnoreExitCode="true">
      <Output TaskParameter="ConsoleOutput" ItemName="_VersionLines" />
      <Output TaskParameter="ExitCode" PropertyName="_EmccVersionExitCode" />
    </Exec>

    <!-- If `emcc -version` failed, then run it again, so we can surface the output as *Errors*. This allows the errors to show up correctly,
         versus trying to use the output lines with the Error task -->
    <Exec Condition="$(_EmccVersionExitCode) != '0'"
          Command="$(_EmccVersionCommand)"
          WorkingDirectory="$(_WasmIntermediateOutputPath)"
          EnvironmentVariables="@(EmscriptenEnvVars)"
          CustomErrorRegularExpression=".*"
          />

    <!-- we want to get the first line from the output, which has the version.
         Rest of the lines are the license -->
    <ItemGroup>
      <_ReversedVersionLines Include="@(_VersionLines->Reverse())" />
    </ItemGroup>
    <PropertyGroup>
      <ActualEmccVersionRaw>%(_ReversedVersionLines.Identity)</ActualEmccVersionRaw>
      <_VersionMismatchMessage>Emscripten version mismatch. The runtime pack in $(MicrosoftNetCoreAppRuntimePackDir) expects '$(RuntimeEmccVersionRaw)', but emcc being used has version '$(ActualEmccVersionRaw)'. This might cause build failures.</_VersionMismatchMessage>
    </PropertyGroup>

    <Warning Condition="'$(RuntimeEmccVersionRaw)' != '$(ActualEmccVersionRaw)'" Text="$(_VersionMismatchMessage)" />
    <Error Condition="'$(RuntimeEmccVersionRaw)' != '$(ActualEmccVersionRaw)'" Text="$(_VersionMismatchMessage)" />
  </Target>

  <Target Name="_CompleteWasmBuildNative" AfterTargets="WasmLinkDotNet">
    <ItemGroup>
      <WasmNativeAsset Include="$(_WasmIntermediateOutputPath)dotnet.native.wasm" />
      <WasmNativeAsset Include="$(_WasmIntermediateOutputPath)dotnet.native.js" />
      <WasmNativeAsset Include="$(_WasmIntermediateOutputPath)dotnet.native.worker.js" Condition="Exists('$(_WasmIntermediateOutputPath)dotnet.native.worker.js')" />
      <WasmNativeAsset Include="$(_WasmIntermediateOutputPath)dotnet.native.js.symbols" Condition="'$(WasmEmitSymbolMap)' == 'true' and Exists('$(_WasmIntermediateOutputPath)dotnet.native.js.symbols')" />
      <_WasmAssembliesInternal Remove="$(_WasmDedupAssembly)"/>
    </ItemGroup>
  </Target>

  <Target Name="_WasmGenerateRunV8Script" Condition="'$(WasmGenerateRunV8Script)' == 'true'">
    <Error Condition="'$(WasmMainJSPath)' == ''" Text="%24(WasmMainJSPath) property needs to be set" />

    <PropertyGroup>
      <WasmRunV8ScriptPath Condition="'$(WasmRunV8ScriptPath)' == ''">$(WasmAppDir)run-v8.sh</WasmRunV8ScriptPath>
      <_WasmMainJSFileName>$([System.IO.Path]::GetFileName('$(WasmMainJSPath)'))</_WasmMainJSFileName>
      <V8ScriptShebang Condition="'$(OS)' != 'Windows_NT'">#!/bin/sh</V8ScriptShebang>
    </PropertyGroup>

    <Error Condition="'$(WasmMainAssemblyFileName)' == ''" Text="%24(WasmMainAssemblyFileName) property needs to be set for generating $(WasmRunV8ScriptPath)." />

    <WriteLinesToFile
      File="$(WasmRunV8ScriptPath)"
      Lines="$(V8ScriptShebang);v8 --expose_wasm --module $(_WasmMainJSFileName) -- ${RUNTIME_ARGS} --run $(WasmMainAssemblyFileName) $*"
      Overwrite="true">
    </WriteLinesToFile>

    <ItemGroup>
      <FileWrites Include="$(WasmRunV8ScriptPath)" />
    </ItemGroup>

    <Exec Condition="'$(OS)' != 'Windows_NT'" Command="chmod a+x &quot;$(WasmRunV8ScriptPath)&quot;" />
  </Target>
</Project>
